home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / memleak_1.0 / memleak.c < prev    next >
C/C++ Source or Header  |  1995-11-05  |  3KB  |  178 lines

  1. /*
  2.  * MemLeak: Test for memory leaks.
  3.  *
  4.  * ©1994 by Francesco Devitt -- Freely distributable without profit.
  5.  *
  6.  * email: ffranc@comp.vuw.ac.nz
  7.  * snail: 29a Kinghorne St, Strathmore, Wellington, NZ.
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <intuition/intuition.h>
  13. #include <libraries/gadtools.h>
  14.  
  15. #include <proto/intuition.h>
  16. #include <proto/gadtools.h>
  17. #include <proto/exec.h>
  18. #include <proto/dos.h>
  19. #include <exec/memory.h>
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. #include "memory.h"
  26.  
  27. static ULONG last_memory;
  28.  
  29. #define ALL_MEMORY (0xfffffff)
  30. #define SAME_LIMIT 50
  31. #define DIFF_DELAY 1
  32.  
  33. const char VerString[] = "$VER: MemLeak 1.0 " __AMIGADATE__;
  34.  
  35.  
  36. /* Patching the gadtools code:
  37.  
  38. memory.h: Reqiures a "void" inserted in the proto for MemoryCloseWindow
  39. memory.c: Replace "clib" with "proto" and "_protos" with "", in the #includes.
  40.  
  41. There are two warnings generated when compiling memory.c, but
  42. it is not worth fixing them each time.
  43.  
  44. */
  45.  
  46.  
  47. void clear_gadget(int gad, char *str)
  48. {
  49.     GT_SetGadgetAttrs(MemoryGadgets[gad], MemoryWnd, NULL,
  50.         GTTX_Text, str,
  51.         TAG_END);
  52. }
  53.  
  54.  
  55. void set_gadget(int gad, char *buf, int num)
  56. {
  57.     sprintf(buf,"%d", num);
  58.     
  59.     GT_SetGadgetAttrs(MemoryGadgets[gad], MemoryWnd, NULL,
  60.         GTTX_Text, buf,
  61.     TAG_END);
  62. }
  63.  
  64.  
  65.  
  66. ULONG memory_available(void)
  67. {
  68.     ULONG prev, mem;
  69.     int same=0;
  70.     
  71.     prev=0;
  72.     
  73.     for (;;)
  74.     {
  75.         /* First flush memory */
  76.         
  77.         APTR tmp = AllocMem(ALL_MEMORY, MEMF_ANY);
  78.         if (tmp) FreeMem(tmp,ALL_MEMORY);  /* Just in case! */
  79.         
  80.         /* determine memory size */
  81.         
  82.         mem = AvailMem(MEMF_ANY);
  83.         
  84.         /* Exit if memory size has not changed for a while */
  85.         
  86.         if (mem != prev) same=0;
  87.         else if (++same >= SAME_LIMIT) break;
  88.         
  89.         prev=mem;
  90.         
  91.         Delay(DIFF_DELAY);
  92.     }
  93.     
  94.     return mem;
  95. }
  96.  
  97.  
  98. ULONG mem_before;
  99. ULONG mem_after;
  100.  
  101. char before_text[32];
  102. char after_text[32];
  103. char diff_text[32];
  104.  
  105.  
  106.  
  107. int BeforeGadClicked( void )
  108. {
  109.     clear_gadget(GD_BeforeText, "wait...");
  110.     clear_gadget(GD_AfterText, NULL);
  111.     clear_gadget(GD_DiffText, NULL);
  112.     
  113.     mem_before = memory_available();
  114.     
  115.     set_gadget(GD_BeforeText, before_text, mem_before);
  116.     
  117.     return 1;
  118. }
  119.  
  120.  
  121. int AfterGadClicked(void)
  122. {
  123.     clear_gadget(GD_AfterText, "wait...");
  124.     clear_gadget(GD_DiffText, NULL);
  125.     
  126.     mem_after = memory_available();
  127.     
  128.     set_gadget(GD_AfterText, after_text, mem_after);
  129.     set_gadget(GD_DiffText, diff_text, mem_before-mem_after);
  130.     
  131.     return 1;
  132. }
  133.  
  134.  
  135.  
  136. void fail(char *str)
  137. {
  138.     struct EasyStruct es=
  139.     {
  140.         sizeof(struct EasyStruct),
  141.         0,
  142.     };
  143.     
  144.     es.es_Title = "Memory check";
  145.     es.es_TextFormat = "Fail: %s";
  146.     es.es_GadgetFormat = "OK";
  147.     
  148.     EasyRequest(0,&es,0,str);
  149.     
  150.     exit(10);
  151. }
  152.  
  153.     
  154. int MemoryCloseWindow( void )
  155. {
  156.     return 0;
  157. }
  158.  
  159.  
  160. void main(void)
  161. {
  162.     if (SetupScreen()) fail("Cannot get screen info");
  163.     if (OpenMemoryWindow())
  164.     {
  165.         CloseDownScreen();
  166.         fail("Cannot open window");
  167.     }
  168.     
  169.     do {
  170.         
  171.         Wait(1L << MemoryWnd->UserPort->mp_SigBit);
  172.         
  173.     } while (HandleMemoryIDCMP());
  174.     
  175.     CloseMemoryWindow();
  176.     CloseDownScreen();
  177. }
  178.